package com.leoren.run.activity.share;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.leoren.run.R;
import com.leoren.run.vo.CommentVo;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 社区分享适配器
 */
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentHolder> {

    private List<CommentVo> commentVoList;
    private Context context;

    public CommentAdapter() {
    }

    public CommentAdapter(List<CommentVo> commentVoList, Context context) {
        this.commentVoList = commentVoList;
        this.context = context;
    }

    static class CommentHolder extends RecyclerView.ViewHolder{
        LinearLayout itemLayoutView;
        CircleImageView headImg;
        TextView usernameText;
        TextView dateText;
        TextView contentText;

        public CommentHolder( View itemView) {
            super(itemView);
            itemLayoutView = (LinearLayout) itemView;
            headImg = itemView.findViewById(R.id.item_comment_head);
            usernameText = itemView.findViewById(R.id.item_comment_username);
            dateText = itemView.findViewById(R.id.item_comment_time);
            contentText = itemView.findViewById(R.id.item_comment_content);
        }
    }

    static class CommentPicHolder extends CommentHolder{

        ImageView picImg;

        public CommentPicHolder(View itemView) {
            super(itemView);
            picImg = itemView.findViewById(R.id.item_comment_pic_img);
        }
    }

    @Override
    public int getItemViewType(int position) {
        CommentVo vo = commentVoList.get(position);
        if(vo.getPicUrl() == null || "".equals(vo.getPicUrl())){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public CommentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType){
            case 0:
                view = LayoutInflater.from(context).inflate(R.layout.item_comment_layout, parent, false);
                return new CommentHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_comment_pic_layout, parent, false);
                return new CommentPicHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(CommentHolder holder, int position) {
        CommentVo vo = commentVoList.get(position);
        Glide.with(context).load(vo.getUserPicUrl()).into(holder.headImg);
        holder.usernameText.setText(vo.getUserName());
        holder.dateText.setText(vo.getDataTimeStr());
        holder.contentText.setText(vo.getContent());
        if(holder instanceof CommentPicHolder){
            CommentPicHolder picHolder = (CommentPicHolder) holder;
            Glide.with(context).load(vo.getPicUrl()).into(picHolder.picImg);
        }

    }

    @Override
    public int getItemCount() {
        return commentVoList.size();
    }



}
